package pp.gateway.adapter.video.upload;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pp.gateway.adapter.video.download.vo.ChunkVO;
import pp.gateway.application.video.download.VideoDownloadService;
import pp.infrastructure.http.Response;
@Slf4j
@RestController
@RequestMapping("/pp/gateway/upload")
public class VideoUploadController {
    @Autowired
    private VideoDownloadService videoService;

    // 分块下载
    @PostMapping("/upload-chunk")
    public Response uploadChunk(ChunkVO input) {
        try {
            if (!videoService.saveChunk(input.toParam())) {
                log.error("{}.{} --> {}", this.getClass().getName(), new Object(){}.getClass().getEnclosingMethod().getName()
                        , "上传失败，参数 input = " + JSON.toJSONString(input));
                return Response.fail();
            }
            return Response.success(input.getChunkNumber(),"分块上传成功: ");
        } catch (Exception e) {
            return Response.fail("分块上传失败: " + e.getMessage());
        }
    }
}
